package com.changpay.zywy.ui.activity.mine

import android.content.DialogInterface
import android.content.Intent
import android.view.View
import com.changpay.zywy.BuildConfig
import com.changpay.zywy.R
import com.changpay.zywy.api.WebApis
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.constant.Constant
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ui.activity.login.LoginActivity
import com.changpay.zywy.ui.activity.web.WebViewActivity
import com.changpay.zywy.utils.CacheUtil
import com.changpay.zywy.utils.DialogUtil
import com.changpay.zywy.utils.Preference
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.title_layout.*

class SettingActivity : BaseActivity(), IView {

    /**
     * token
     */
    var isFirst: Boolean by Preference(Constant.IS_FIRST, true)
    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {

    }

    override fun showMsg(msg: String) {

    }

    override fun showError(errorMsg: String) {

    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.setting_one -> startActivity(ChangePhonePwActivity::class.java)
            R.id.setting_two -> Intent(this, GetCodeActivity::class.java).run {
                putExtra("captcha_type", "7")
                putExtra("title", "请先通过短信验证")
                startActivity(this)
            }
            R.id.setting_three -> startActivity(FeedBackActivity::class.java)
            R.id.setting_four-> WebViewActivity.startActivity(this,WebApis.about)
            R.id.setting_five -> {
//                DialogUtil.getMessageDialog(this, "确定清除缓存",DialogInterface.OnClickListener())
                val listener = DialogInterface.OnClickListener { _, _ ->
                    CacheUtil.cleanAllCache(this)
                    cacheSizeTv.text = CacheUtil.getCacheSize(this)
                    showToast("清除完毕")
                }
                DialogUtil.getMessageDialog(this, "确定清除缓存", listener).show()
            }

//            R.id.setting_six->
            R.id.setting_out_login -> {
                DialogUtil.showDefaultDialog(this, "", "确定退出登录?", "确定", "取消",
                    object : DialogUtil.DialogImpl {
                        override fun onOk() {
                            val intent2 = Intent(this@SettingActivity, LoginActivity::class.java)
                            Preference.clearPreference()
                            isFirst = false
                            startActivity(intent2)
                        }
                    })
            }
        }
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_setting
    }

    override fun initData() {

    }

    override fun initView() {
        tv_title.text = "设置"
        setting_one.setOnClickListener(onClickListener)
        setting_two.setOnClickListener(onClickListener)
        setting_three.setOnClickListener(onClickListener)
        setting_four.setOnClickListener(onClickListener)
        setting_five.setOnClickListener(onClickListener)
        setting_six.setOnClickListener(onClickListener)
        setting_out_login.setOnClickListener(onClickListener)
        iv_back.setOnClickListener(onClickListener)
        setting_six.text = "当前版本 ${BuildConfig.VERSION_NAME}"
        cacheSizeTv.text = CacheUtil.getCacheSize(this)
    }

    override fun start() {

    }
}
